import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;
/**
 * Table model for the daily appointment sheet.
 * @author Vishnu
 *
 */
public class DailyApptModel extends AbstractTableModel {

	private MedAppController mac;
	private Connection con;
	private String[] columnNames = {"Date and Time", "Reason","Doctor Name"};
	private Object[][] data;
	private ArrayList<String> dateAndTime;
	private ArrayList<String> reasons;
	private ArrayList<String> doctorNames;
	
	/**
	 * Constructor for the DailyApptModel
	 * @param mac mac MedAppController object
	 */
	public DailyApptModel(MedAppController mac) {
		this.mac = mac;
		con = mac.getCon();
		
		dateAndTime = new ArrayList<String>();
		reasons = new ArrayList<String>();
		doctorNames = new ArrayList<String>();
		
		String sql = "SELECT * FROM  `appointment` WHERE DATE(  `DATEANDTIME` ) = CURDATE( ) LIMIT 0 , 30";
		
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			int count = 0;
			
			while(rs.next()){
				dateAndTime.add(rs.getString(1));
				reasons.add(rs.getString(2));
				doctorNames.add(rs.getString(4)+" "+rs.getString(5));
				count++;
			}
			
			data = new Object[dateAndTime.size()][3];
			
			for(int i = 0;i<count;i++){
				data[i][0] = dateAndTime.get(i);
			}
			
			for(int i = 0;i<count;i++){
				data[i][1] = reasons.get(i);
			}
			
			for(int i = 0;i<count;i++){
				data[i][2] = doctorNames.get(i);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return data.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int columnIndex){
		// TODO Auto-generated method stub
//		if(data[rowIndex][columnIndex]==null){
//			return null;
//		}
		return data[rowIndex][columnIndex];
	}

	public String getColumnName(int col) {
        return columnNames[col];
    }
}
